/**
 * https://leetcode.cn/problems/maximize-palindrome-length-from-subsequences/submissions/
 * 1771.由子序列构造的最长回文串的长度 2182
 * difficulty 陈贝纯 11.21
 * 区间dp
 */

class Solution {
public:
    int longestPalindrome(string word1, string word2) {
        string ss=word1+word2;
        int n=ss.size();
        int ans=0;
        vector<vector<int>> dp(n,vector<int>(n,0));
        for(int i=n-1;i>=0;--i){
            dp[i][i]=1;
            for(int j=i+1;j<n;++j){
                if(ss[i]==ss[j]){
                    dp[i][j]=dp[i+1][j-1]+2;
                    if(i<word1.size()&&j>=word1.size())
                    ans=max(ans,dp[i][j]);
                }
                else
                dp[i][j]=max(dp[i+1][j],dp[i][j-1]);
            }
        }
        
        return ans;
    }
};